package com.hgshop.yudongyang.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.hgshop.yudongyang.domain.Brand;
import com.hgshop.yudongyang.domain.Sku;
import com.hgshop.yudongyang.domain.SkuVo;
import com.hgshop.yudongyang.domain.Spec;
import com.hgshop.yudongyang.domain.SpecOption;
import com.hgshop.yudongyang.domain.Spu;
import com.hgshop.yudongyang.service.BrandService;
import com.hgshop.yudongyang.service.SkuService;
import com.hgshop.yudongyang.service.SpecService;
import com.hgshop.yudongyang.service.SpuService;

@Controller
@RequestMapping("sku")
public class SkuController {

	@Reference
	SkuService skuService;
	
	@Reference
	SpuService spuService;
	
	@Reference
	SpecService specService;
	
	@Value("${file.path}")
	String path;
	
	@PostMapping("update")
	@ResponseBody
	public boolean update(Sku sku, MultipartFile cartThumbnailFile, MultipartFile imageFile,
			@RequestParam("specId") Integer[] specIds, @RequestParam("optionId") Integer[] optionIds) {
		try {
			String cartThumbnailFilePath = uploadPic(cartThumbnailFile);
			if(cartThumbnailFilePath!=null && cartThumbnailFilePath!="") {
				sku.setCartThumbnail(cartThumbnailFilePath);
			}
			
			String imageFilePath = uploadPic(imageFile);
			if(imageFilePath!=null && imageFilePath!="") {
				sku.setImage(imageFilePath);
			}
			skuService.update(sku,specIds,optionIds);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	@GetMapping("toupdate")
	public String toupdate(int id,Model model) {
		Sku sku = skuService.findSkuById(id);
		model.addAttribute("sku", sku);
		//获取品牌数据集合
				PageInfo<Spec> specList = specService.specList(0, Integer.MAX_VALUE);
				model.addAttribute("specList", specList.getList());
		List<SpecOption> optionList = sku.getOptionList();
				//获取品牌类型数据集合
		for (SpecOption specOption : optionList) {
			System.out.println(specOption);
		}
		return "sku/update";
	}
	
	@PostMapping("add")
	@ResponseBody
	public boolean add(Sku sku,MultipartFile cartThumbnailFile,MultipartFile imageFile,
			@RequestParam("specId") Integer[] specIds, @RequestParam("optionId") Integer[] optionIds) {
		System.err.println(Arrays.toString(specIds));
		System.err.println(Arrays.toString(optionIds));

		try {
			String imageFilePath = uploadPic(imageFile);
			String cartThumbnailFilePath = uploadPic(cartThumbnailFile);
			sku.setImage(imageFilePath);
			sku.setCartThumbnail(cartThumbnailFilePath);
			skuService.add(sku, specIds, optionIds);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	
	}
	
	@RequestMapping("list")
	public String list(Model model,SkuVo vo,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize) {
		PageInfo<Sku> info = skuService.list(pageNum,pageSize,vo);
		model.addAttribute("info", info);
		model.addAttribute("vo", vo);
		return "sku/list";
	}
	/**
	 * 
	 * @Title: toAdd 
	 * @Description: 去添加
	 * @param id
	 * @param model
	 * @return
	 * @return: String
	 */
	@GetMapping("toadd")
	public String toAdd(Integer id,Model model) {
		Spu one = spuService.getOne(id);
		model.addAttribute("spu", one);
		//获取品牌数据集合
		PageInfo<Spec> specList = specService.specList(0, Integer.MAX_VALUE);
		model.addAttribute("specList", specList.getList());
		//获取品牌类型数据集合
		
		return "sku/add";
	}
	
	// 图片上传的方法=================================================================
		public String uploadPic(MultipartFile picFileName) {
			String newFileName = "";
			if (picFileName != null && !picFileName.isEmpty()) {

				String originalFilename = picFileName.getOriginalFilename();// 获取原始文件的名称,aaa.jpg
				String subName = originalFilename.substring(originalFilename.lastIndexOf("."));// .jpg
				String preName = UUID.randomUUID().toString();// 随机生成一个字符串作为文件的名称
				newFileName = preName + subName;

				File dest = new File(path, newFileName);
				try {
					picFileName.transferTo(dest);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			return newFileName;

		}
		// 图片上传的方法=================================================================
}
